// GYWalkAids.cpp : Defines the class behaviors for the application.
//

#include "StdAfx.h"
#include "GYWalkAidApp.h"
#include "../Login/GYWalkAidDlg.h"
#include "../Auth/Auth.h"
#include "../Common/CommonFun.h"
#include "../Common/GYDataSet.h"
#include "NavigateDlg.h"
#include "CustomMsgCenter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGYWalkAidsApp

BEGIN_MESSAGE_MAP(CGYWalkAidsApp, CWinApp)
	//{{AFX_MSG_MAP(CGYWalkAidsApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGYWalkAidsApp construction

CGYWalkAidsApp::CGYWalkAidsApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	m_pFrameDlg				= NULL;
	m_pCustMsgCenter		= NULL;
	m_dwSerialNo			= 0;
	m_dwUserRoleID			= 0;
	m_hGaitWnd				= NULL;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CGYWalkAidsApp object

CGYWalkAidsApp theApp;
volatile BOOL g_bAppQuit = FALSE;

/////////////////////////////////////////////////////////////////////////////
// CGYWalkAidsApp initialization

BOOL CGYWalkAidsApp::InitInstance()
{
	AfxEnableControlContainer();

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	//  Check hardware id
	CString strSQL;
	strSQL.Format(_T("SELECT ComputerNo, ConfigValue FROM SysConfig WHERE ConfigName='HardwareID'"));

	BOOL bValid = FALSE;
	CGYDataSet setSysConfig;
	setSysConfig.OpenTable(strSQL);
	for(; !setSysConfig.IsEOF(); setSysConfig.MoveNext())
	{
		CString strComputerID;
		setSysConfig.GetFieldValue(_T("ConfigValue"), strComputerID);
		if(strComputerID == CAuth::GetComputerID())
		{
			bValid = TRUE;
			break;
		}
	}

	if(!bValid)
	{
		GYMsgBox(IDS_LGN_SYSNOTMATCH, MB_OK|MB_ICONSTOP);
		return FALSE;
	}

 	CString strComputerNo;
	setSysConfig.GetFieldValue(_T("ComputerNo"), strComputerNo);

	strSQL.Format(_T("SELECT ConfigValue FROM SysConfig WHERE ComputerNo=%s AND ConfigName='SoftwareSerialNo'"), strComputerNo);
	setSysConfig.OpenTable(strSQL);
	if(setSysConfig.IsEOF())
	{
		GYMsgBox(IDS_NAVI_NO_SN, MB_OK|MB_ICONSTOP);
		return FALSE;
	}

	setSysConfig.GetFieldValue(_T("ConfigValue"), m_strSoftwareSN);

	strSQL.Format(_T("SELECT ConfigValue FROM SysConfig WHERE ComputerNo=%s AND ConfigName='VersionNo'"), strComputerNo);
	setSysConfig.OpenTable(strSQL);
	if(setSysConfig.IsEOF())
	{
		GYMsgBox(IDS_SYS_NO_VERSION_NO, MB_OK|MB_ICONSTOP);
		return FALSE;
	}

	CString strVersionNo;
	setSysConfig.GetFieldValue(_T("ConfigValue"), strVersionNo);
	setSysConfig.Close();

	m_pCustMsgCenter = new CCustomMsgCenter();
	if(!m_pCustMsgCenter)
		return FALSE;

	m_pCustMsgCenter->Init();

	if( !GetApp()->GetWAMan()->Init() )
		return FALSE;

	//  Login dialog
	{
		CGYWalkAidDlg dlgLogin;
		if (dlgLogin.DoModal() != IDOK)
		{
			GetApp()->GetWAMan()->Uninit();

			m_pCustMsgCenter->Uninit();
			SAFE_DELETE(m_pCustMsgCenter);

			return FALSE;
		}
		
		m_strUserName		= dlgLogin.GetLoginUser();
		m_dwUserRoleID		= dlgLogin.GetLoginUserRoleID();
		m_strUserRealName	= dlgLogin.GetUserRealName();
	}

	//  Navigate dialog
	{
		CNavigateDlg dlgNavi;
		m_pFrameDlg = &dlgNavi;
		m_pMainWnd = &dlgNavi;

		dlgNavi.SetLoginUser(m_strUserName);
		dlgNavi.SetSerialNo(m_strSoftwareSN);
		dlgNavi.SetVersionNo(strVersionNo);
		dlgNavi.DoModal();
	}

	GetApp()->GetWAMan()->Uninit();

	m_pCustMsgCenter->Uninit();
	SAFE_DELETE(m_pCustMsgCenter);

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
